package com.learn.join;

import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.JoinFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;

/**
 * @create: 2023-04-24 10:58
 * @author: Mr.Du
 * --------------
 * @notes:
 **/
public class TumblingWindowJoinDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //checkpoint关掉或者设置的小一点
        //env.enableCheckpointing(10000);
        env.setParallelism(1);
        DataStreamSource<String> mainOrderSource = env.socketTextStream("node1", 9999);
        DataStreamSource<String> detailOrderSource = env.socketTextStream("node1", 9998);

        SingleOutputStreamOperator<MainOrder> mainOrderStream = mainOrderSource.map(new MapFunction<String, MainOrder>() {
            @Override
            public MainOrder map(String s) throws Exception {
                String[] arr = s.split(",");
                return new MainOrder(arr[0], arr[1], arr[2], arr[3], Double.parseDouble(arr[4]));
            }
        }).assignTimestampsAndWatermarks(WatermarkStrategy.<MainOrder>forBoundedOutOfOrderness(Duration.ZERO)
                .withTimestampAssigner(new SerializableTimestampAssigner<MainOrder>() {
                    @Override
                    public long extractTimestamp(MainOrder mainOrder, long l) {
                        System.out.println("MainOrder="+mainOrder);
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        try {
                            return format.parse(mainOrder.getOrderTime()).getTime();
                        } catch (ParseException e) {
                            e.printStackTrace();
                            return 0L;
                        }
                    }
                }));

        SingleOutputStreamOperator<DetailOrder> detailOrderStream = detailOrderSource.map(new MapFunction<String, DetailOrder>() {
            @Override
            public DetailOrder map(String s) throws Exception {
                String[] arr = s.split(",");
                return new DetailOrder(arr[0], arr[1], arr[2], arr[3], Integer.parseInt(arr[4]), Double.parseDouble(arr[5]));
            }
        }).assignTimestampsAndWatermarks(WatermarkStrategy.<DetailOrder>forBoundedOutOfOrderness(Duration.ZERO)
                .withTimestampAssigner(new SerializableTimestampAssigner<DetailOrder>() {
                    @Override
                    public long extractTimestamp(DetailOrder detailOrder, long l) {
                        System.out.println("DetailOrder="+detailOrder);
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        try {
                            return format.parse(detailOrder.getDetailTime()).getTime();
                        } catch (ParseException e) {
                            e.printStackTrace();
                            return 0L;
                        }
                    }
                }));

        mainOrderStream
                .join(detailOrderStream)
                .where(new KeySelector<MainOrder, String>() {
                    @Override
                    public String getKey(MainOrder mainOrder) throws Exception {
                        System.out.println("mainOrder.getOrderId() = " + mainOrder.getOrderId());
                        return mainOrder.getOrderId();
                    }
                }).equalTo(new KeySelector<DetailOrder, String>() {
                    @Override
                    public String getKey(DetailOrder detailOrder) throws Exception {
                        System.out.println("detailOrder.getOrderId() = " + detailOrder.getOrderId());
                        return detailOrder.getOrderId();
                    }
                })
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .apply(new JoinFunction<MainOrder, DetailOrder, DwdOrder>() {
                    @Override
                    public DwdOrder join(MainOrder mainOrder, DetailOrder detailOrder) {
                        System.out.println("mainOrder=" + mainOrder);
                        System.out.println("detailOrder=" + detailOrder);
                        return new DwdOrder(mainOrder.getOrderTime(),mainOrder.getOrderId(),
                                mainOrder.getUserId(), mainOrder.getAddress(),mainOrder.getOrderMoney(),
                                detailOrder.getDetailTime(),detailOrder.getDetailId(),
                                detailOrder.getGoodsName(),detailOrder.getGoodsNumber(), detailOrder.getDetailMoney());
                    }
                }).print();

        env.execute();
    }
}

